package cn.cj.erp.serviceimpl.system;

import cn.cj.erp.common.redis.RedisCache;
import cn.cj.erp.common.redis.RedisKeyEnum;
import cn.cj.erp.common.utils.Convert;
import cn.cj.erp.bo.system.SysConfigBo;
import cn.cj.erp.service.system.SysConfigService;
import cn.cj.erp.serviceimpl.base.ServiceImpl;
import cn.hutool.core.util.ObjectUtil;
import cn.cj.erp.entity.system.SysConfig;
import cn.cj.erp.dao.system.SysConfigDao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统配置信息
 *
 * @author chenjian
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfig, SysConfigBo> implements SysConfigService {
    @Resource
    private SysConfigDao sysConfigDao;
    @Resource
    private RedisCache redisCache;

    @Override
    public SysConfigDao getBaseMapper() {
        return sysConfigDao;
    }

    @Override
    public SysConfig convert2Entity(SysConfigBo sysConfigBo) {
        return Convert.convert(sysConfigBo, SysConfig.class);
    }

    @Override
    public SysConfigBo convert2Bo(SysConfig sysConfig) {
        return Convert.convert(sysConfig, SysConfigBo.class);
    }

    @Override
    public boolean save(SysConfigBo bo) {
        super.save(bo);
        redisCache.set(RedisKeyEnum.SYSTEM_CONFIG.getKey(bo.getConfigKey()), bo.getConfigValue());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SysConfigBo bo) {
        SysConfig db = getBaseMapper().selectById(bo.getId());
        redisCache.delete(RedisKeyEnum.SYSTEM_CONFIG.getKey(db.getConfigKey()));
        super.updateById(bo);
        redisCache.set(RedisKeyEnum.SYSTEM_CONFIG.getKey(bo.getConfigKey()), bo.getConfigValue());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateValue(String key, String value) {
        getBaseMapper().updateValueByKey(key, value);
        redisCache.set(RedisKeyEnum.SYSTEM_CONFIG.getKey(key), value);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(List<Long> ids) {
        List<SysConfig> dbList = getBaseMapper().selectBatchIds(ids);
        if (!super.removeByIds(ids)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        for (SysConfig sysConfig : dbList) {
            redisCache.delete(RedisKeyEnum.SYSTEM_CONFIG.getKey(sysConfig.getConfigKey()));
        }
        return true;
    }

    @Override
    public String getValue(String key) {
        String value = redisCache.get(RedisKeyEnum.SYSTEM_CONFIG.getKey(key));
        if (value != null) {
            return value;
        }
        SysConfig config = getBaseMapper().getByKey(key);
        SysConfigBo bo = convert2Bo(config);

        redisCache.set(RedisKeyEnum.SYSTEM_CONFIG.getKey(bo.getConfigKey()), bo.getConfigValue());
        return bo.getConfigValue();
    }

    @Override
    public Boolean refreshCache() {
        for (SysConfig sysConfig : getBaseMapper().selectList(null)) {
            redisCache.delete(RedisKeyEnum.SYSTEM_CONFIG.getKey(sysConfig.getConfigKey()));
        }
        return true;
    }

    @Override
    public Boolean loadingConfigCache() {
        for (SysConfig sysConfig : getBaseMapper().selectList(null)) {
            redisCache.set(RedisKeyEnum.SYSTEM_CONFIG.getKey(sysConfig.getConfigKey()), sysConfig.getConfigValue());
        }
        return true;
    }

    @Override
    public Boolean keyNotExist(SysConfigBo bo) {
        int count = getBaseMapper().selectCount(new LambdaQueryWrapper<SysConfig>()
                .ne(ObjectUtil.isNotNull(bo.getId()), SysConfig::getId, bo.getId())
                .eq(SysConfig::getConfigKey, bo.getConfigKey()));
        return count <= 0;
    }
}
